<template>
  <div class="home-cotanier">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button class="home-search" slot="title" icon="search" round type="info" size="small" to="/search">搜索</van-button>
    </van-nav-bar>
    <!-- 频道列表 -->
    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
      <van-tab :title="item.name" v-for="item in userInfo" :key="item.id">
        <ArticleList :item="item"></ArticleList>
      </van-tab>
      <div class="tabs-btn" slot="nav-right"></div>
      <div slot="nav-right" class="hanberger-btn" @click="isEditShow = true">
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>

    <!-- 频道编辑列表 -->
    <van-popup v-model="isEditShow" closeable close-icon-position="top-left" position="bottom" :style="{ height: '100%' }">
      <!-- 汉堡组件 -->
      <channel-edit :channel="userInfo" :active="active" @onChannel="onChannelClick"> </channel-edit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannelAPI } from '@/api/LoginAPI.js'
import ArticleList from '@/views/Home/component/Article-list.vue'
import channelEdit from '@/views/Home/component/channel-edit.vue'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage.js'
export default {
  name: 'HomeIndex',
  data() {
    return {
      active: 0,
      userInfo: [],
      isEditShow: false //控制弹出层是否展示
    }
  },
  computed: {
    ...mapState(['user'])
  },
  created() {
    this.getUserChannel()
  },
  methods: {
    async getUserChannel() {
      try {
        // const { data } = await getUserChannelAPI()
        // this.userInfo = data.data.channels
        // console.log(data)
        // 是否登录
        let userInfo = []
        if (this.user) {
          // 已登录，请求获取用户频道列表
          const { data } = await getUserChannelAPI()
          userInfo = data.data.channels
        } else {
          const localChannel = getItem('TOUTIAO_ITEM')
          // 未登录，是否有本地存储频道列表
          if (localChannel) {
            // 有，获取使用
            userInfo = localChannel
          } else {
            // 没有，请求获取推荐频道数据
            const { data } = await getUserChannelAPI()
            userInfo = data.data.channels
          }
        }
        this.userInfo = userInfo
      } catch (err) {
        console.log(err, '获取失败')
      }
    },
    onChannelClick(index, isEditShow = true) {
      this.active = index
      this.isEditShow = isEditShow
    }
  },
  components: {
    ArticleList,
    channelEdit
  }
}
</script>
<style lang="less" scoped>
.home-cotanier {
  padding-bottom: 100px;
  padding-top: 174px;
  .home-search {
    width: 555px;
    height: 64px;
    max-width: unset;
    border-radius: 32px;
    background-color: #5babfb;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  /deep/ .channel-tabs {
    .van-tabs__wrap {
      height: 83px;
      position: fixed;
      z-index: 1;
      top: 92px;
      left: 0;
      right: 0;
    }
    .van-tab {
      border-right: 1px solid #edeff3;
      min-width: 200px;
      font-size: 28px;
      color: #777;
    }
    .van-tabs__line {
      width: 31px;
      height: 6px;
      background-color: #3296fa;
      bottom: 8px;
    }
    .van-tabs__nav--line {
      padding: 0;
    }
    .van-tab--active {
      font-size: 30px;
      color: #333333;
    }
    .tabs-btn {
      flex-shrink: 0;
      width: 66px;
      height: 82px;
    }
    .hanberger-btn {
      position: fixed;
      display: flex;
      justify-content: center;
      align-items: center;
      right: 0;
      width: 66px;
      height: 82px;
      background-color: #ffffff;
      opacity: 0.902;
      i.toutiao {
        font-size: 40px;
      }
      &::before {
        position: absolute;
        left: 0;
        content: '';
        width: 1px;
        height: 100%;
        background: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
      }
    }
  }
}
</style>
